/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.org.energy.vkek.site.web;

import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ua.org.energy.vkek.site.model.BaseEntity;

/**
 *
 * @author moroz
 */
public class BaseJsonServlet extends HttpServlet {

    public class EntityUIDJson<T extends BaseEntity> {

        public EntityUIDJson(String operation, T ent) {
            //TODO: dirty hack to avoid stackoverflow in gson
            if (ent != null) {
                this.ent = ent.safeClone();
            }
            this.Ok = operation;

        }
        public String Ok;
        public T ent;
    }

    public static void printOk(EntityUIDJson uidjson, PrintWriter out) {
        Gson gson = new Gson();
        String json = gson.toJson(uidjson);
        assert (json != null);
        out.print(json);
    }

    public static void printEntList(List<? extends BaseEntity> list, PrintWriter out) {
        if (list != null) {//TODO: this is dirty hack to avoid stackoverflow problem
            list = getSafeList(list);
        }
        Gson gson = new Gson();
        String json = gson.toJson(list);
        out.print(json);
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    public static boolean checkParamValue(String paramvalue) {
        return ((paramvalue != null) && (!paramvalue.isEmpty()) && (!paramvalue.equalsIgnoreCase("undefined")));
    }

    public static <E extends BaseEntity> List<E> getSafeList(List<E> val) {
        List<E> result = new ArrayList<E>(val.size() + 1);
        for (E t : val) {
            E e = t.safeClone();
            result.add(e);
        }
        return result;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    // </editor-fold>
}
